home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / lut / mapapply.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-04  |  5.7 KB  |  234 lines

  1.  
  2. /*    Copyright (c) 1989 Michael Landy
  3.  
  4. Disclaimer:  No guarantees of performance accompany this software,
  5. nor is any responsibility assumed on the part of the authors.  All the
  6. software has been tested extensively and every effort has been made to
  7. insure its reliability.   */
  8.  
  9. /*
  10.  * mapapply.c - Apply a previously generated map to each pixel of a frame.
  11.  *
  12.  * usage: mapapply map_name < frame > new_frame
  13.  *
  14.  * For byte images, pixels are renormalized to lie between 0 and 255.  For
  15.  * integer and float images, the output is a float image and no
  16.  * renormalization is performed.
  17.  *
  18.  * to load: cc -o mapapply mapapply.c -lhips -lm
  19.  *
  20.  * Yoav Cohen 2/16/82
  21.  * added int/float - Mike Landy - 3/16/89
  22.  *
  23.  *  modified to use look-up table for byte and short images:
  24.  *     Brian Tierney, LBL 10/90
  25.  *
  26.  *  modified to generate look-up table for byte and short images, but not
  27.  *  apply the table to the image.  Therefore, this is to be used in
  28.  *  conjunction with mapapply to generate an image:
  29.  *     Bryan Skene, LBL 1/29/91
  30.  *
  31.  */
  32.  
  33. #include <hipl_format.h>
  34. #include <math.h>
  35. #include <stdio.h>
  36.  
  37. #define MAXSHORT 32768
  38.  
  39. char      forms[80];
  40.  
  41. unsigned char *pic;
  42. short    *spic;
  43. float    *fpic;
  44.  
  45. FILE     *fp;
  46. unsigned char byte_blut[256];
  47. short    *short_slut;
  48. float     byte_flut[256], *short_flut;
  49.  
  50. main(argc, argv)
  51.     int       argc;
  52.     char     *argv[];
  53.  
  54. {
  55.     struct header hd;
  56.     int       i, form, tblform, npix, numentries, bentry;
  57.     short     sentry;
  58.     float     fentry;
  59.     char      mapname[80];
  60.  
  61.     Progname = strsave(*argv);
  62.     read_header(&hd);
  63.     form = hd.pixel_format;
  64.     npix = hd.orows * hd.ocols;
  65.  
  66.     strcpy(mapname, argv[1]);
  67.     fp = fopen(mapname, "r");
  68.     fscanf(fp, "%d", &numentries);
  69.     fscanf(fp, "%d", &tblform);
  70.  
  71.     fprintf(stderr, "number of entries in table = %d\n", numentries);
  72.     strform(form);
  73.     fprintf(stderr, "from : %s\n", forms);
  74.     strform(tblform);
  75.     fprintf(stderr, "to   : %s\n", forms);
  76.  
  77.     if (form == PFBYTE) {
  78.     if (tblform == PFBYTE) {
  79.         pic = (unsigned char *) halloc(npix, sizeof(char));
  80.         update_header(&hd, argc, argv);
  81.         write_header(&hd);
  82.         for (i = 0; i < numentries; i++) {
  83.         fscanf(fp, "%d\n", &bentry);
  84.         byte_blut[i] = bentry;
  85.         }
  86.         bmapapplyb(hd.num_frame, hd.orows, hd.ocols);
  87.     } else if (tblform == PFFLOAT) {
  88.         hd.pixel_format = PFFLOAT;
  89.         pic = (unsigned char *) halloc(npix, sizeof(char));
  90.         fpic = (float *) halloc(npix, sizeof(float));
  91.         update_header(&hd, argc, argv);
  92.         write_header(&hd);
  93.         for (i = 0; i < numentries; i++) {
  94.         fscanf(fp, "%f\n", &fentry);
  95.         byte_flut[i] = fentry;
  96.         }
  97.         bmapapplyf(hd.num_frame, hd.orows, hd.ocols);
  98.     }
  99.     } else if (form == PFSHORT) {
  100.     if (tblform == PFSHORT) {
  101.         spic = (short *) halloc(npix, sizeof(short));
  102.         short_slut = (short *) halloc(numentries, sizeof(short));
  103.         update_header(&hd, argc, argv);
  104.         write_header(&hd);
  105.         fprintf(stderr, "S to S:number of entries = %d\n", numentries);
  106.         for (i = 0; i < numentries; i++) {
  107.         fscanf(fp, "%f\n", &sentry);
  108.         short_slut[i] = (short) sentry;
  109.         }
  110.         smapapplys(hd.num_frame, hd.orows, hd.ocols);
  111.     } else if (tblform == PFFLOAT) {
  112.         hd.pixel_format = PFFLOAT;
  113.         spic = (short *) halloc(npix, sizeof(short));
  114.         fpic = (float *) halloc(npix, sizeof(float));
  115.         short_flut = (float *) halloc(numentries, sizeof(float));
  116.         update_header(&hd, argc, argv);
  117.         write_header(&hd);
  118.         for (i = 0; i < numentries; i++) {
  119.         fscanf(fp, "%f\n", &fentry);
  120.         short_flut[i] = fentry;
  121.         }
  122.         smapapplyf(hd.num_frame, hd.orows, hd.ocols);
  123.     }
  124.     } else
  125.     perr(HE_MSG, "input image format must be byte or short");
  126.  
  127.     close(fp);
  128.     return (0);
  129. }
  130.  
  131. bmapapplyb(fr, r, c)
  132.     int       fr, r, c;
  133.  
  134. {
  135.     int       j, i, rc;
  136.     unsigned char *ppic;
  137.  
  138.     rc = r * c;
  139.     for (j = 0; j < fr; j++) {
  140.     if (fread(pic, rc * sizeof(unsigned char), 1, stdin) != 1)
  141.         perr(HE_MSG, "error during read");
  142.     ppic = pic;
  143.     for (i = 0; i < rc; i++)
  144.         *ppic++ = byte_blut[*ppic];
  145.     if (fwrite(pic, rc * sizeof(char), 1, stdout) != 1)
  146.         perr(HE_MSG, "error during write");
  147.     }
  148.     return (0);
  149. }
  150.  
  151. bmapapplyf(fr, r, c)
  152.     int       fr, r, c;
  153.  
  154. {
  155.     int       j, i, rc;
  156.     unsigned char *ppic;
  157.     float    *pfpic;
  158.  
  159.     rc = r * c;
  160.     for (j = 0; j < fr; j++) {
  161.     if (fread(pic, rc * sizeof(unsigned char), 1, stdin) != 1)
  162.         perr(HE_MSG, "error during read");
  163.     ppic = pic;
  164.     pfpic = fpic;
  165.     for (i = 0; i < rc; i++)
  166.         *pfpic++ = byte_flut[*ppic++];
  167.     if (fwrite(fpic, rc * sizeof(float), 1, stdout) != 1)
  168.         perr(HE_MSG, "error during write");
  169.     }
  170.     return (0);
  171. }
  172.  
  173.  
  174. smapapplys(fr, r, c)
  175.     int       fr, r, c;
  176.  
  177. {
  178.     int       j, i, rc;
  179.     short    *pspic;
  180.  
  181.     rc = r * c;
  182.     for (j = 0; j < fr; j++) {
  183.     if (fread(spic, rc * sizeof(short), 1, stdin) != 1)
  184.         perr(HE_MSG, "error during read");
  185.     fprintf(stderr, "Ya boy!\n");
  186.     pspic = spic;
  187.     for (i = 0; i < rc; i++)
  188.         *pspic++ = short_slut[*pspic];
  189.     if (fwrite(spic, rc * sizeof(short), 1, stdout) != 1)
  190.         perr(HE_MSG, "error during write");
  191.     }
  192.     return (0);
  193. }
  194.  
  195. smapapplyf(fr, r, c)
  196.     int       fr, r, c;
  197.  
  198. {
  199.     int       j, i, rc;
  200.     short    *pspic;
  201.     float    *pfpic;
  202.  
  203.     rc = r * c;
  204.     for (j = 0; j < fr; j++) {
  205.     if (fread(spic, rc * sizeof(short), 1, stdin) != 1)
  206.         perr(HE_MSG, "error during read");
  207.     pspic = spic;
  208.     pfpic = fpic;
  209.     for (i = 0; i < rc; i++)
  210.         *pfpic++ = short_flut[*pspic++];
  211.     if (fwrite(fpic, rc * sizeof(float), 1, stdout) != 1)
  212.         perr(HE_MSG, "error during write");
  213.     }
  214.     return (0);
  215. }
  216.  
  217. strform(formi)
  218.     int       formi;
  219. {
  220.     switch (formi) {
  221.     case PFBYTE:
  222.     strcpy(forms, "BYTE");
  223.     break;
  224.     case PFSHORT:
  225.     strcpy(forms, "SHORT");
  226.     break;
  227.     case PFFLOAT:
  228.     strcpy(forms, "FLOAT");
  229.     break;
  230.     default:
  231.     strcpy(forms, "UNKNOWN");
  232.     }
  233. }
  234.